package com.dkd.handler;

import cn.elegent.ac.ACHandler;
import cn.elegent.ac.annotation.Topic;
import cn.elegent.pay.ElegentPay;
import com.dkd.config.TopicConfig;
import com.dkd.config.constant.OrderStatus;
import com.dkd.contract.OrderCheck;
import com.dkd.entity.OrderEntity;
import com.dkd.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @author DiXiaoNie
 * @since 2023/2/4 18:35
 */
@Topic(TopicConfig.ORDER_CHECK_TOPIC)
@Slf4j
public class OrderCheckHandler implements ACHandler<OrderCheck> {

    @Autowired
    private OrderService orderService;

    @Autowired
    private ElegentPay elegentPay;

    @Override
    public void process(String topic, OrderCheck orderCheck) throws Exception {
        if (orderCheck == null || StringUtils.isBlank(orderCheck.getOrderNo())) {
            return;
        }
        //查询订单
        OrderEntity orderEntity = this.orderService.getByOrderNo(orderCheck.getOrderNo());
        if (orderCheck == null) {
            return;
        }
        if (orderEntity.getStatus().equals(OrderStatus.ORDER_STATUS_CREATE)) {
            log.info("订单无效处理 订单号:{}", orderCheck.getOrderNo());
            //无效状态
            orderEntity.setStatus(OrderStatus.ORDER_STATUS_INVALID);
            this.orderService.updateById(orderEntity);
            //关闭支付
            elegentPay.closePay(orderEntity.getOrderNo(), orderEntity.getPayType());
        }
    }
}

